package cn.crism.chatme.service.impl;

import cn.crism.chatme.enums.MsgActionEnum;
import cn.crism.chatme.mapper.FriendRelationMapper;
import cn.crism.chatme.mapper.FriendsReqMapper;
import cn.crism.chatme.mapper.UserInfoMapperCustom;
import cn.crism.chatme.netty.DataContent;
import cn.crism.chatme.netty.UserChannelRel;
import cn.crism.chatme.pojo.FriendRelation;
import cn.crism.chatme.pojo.FriendsReq;
import cn.crism.chatme.service.FriendService;
import cn.crism.chatme.utils.JsonUtils;
import cn.crism.chatme.vo.FriendRequestVO;
import cn.crism.chatme.vo.FriendVO;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @CreateDate: 2018/12/27 2:50 PM
 * @Description:
 * @Author: Crism
 */

@Service
public class FriendServiceImpl implements FriendService {

    @Autowired
    private UserInfoMapperCustom userInfoMapperCustom;

    @Autowired
    private Sid sid;

    @Autowired
    private FriendsReqMapper friendsReqMapper;
    @Autowired
    private FriendRelationMapper friendRelationMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<FriendRequestVO> queryFriendRequestList(String replyUserId) {
        return userInfoMapperCustom.queryFriendRequestList(replyUserId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteFriendRequest(String accessUserId, String sendUserId) {
        Example friendReq = new Example(FriendsReq.class);
        Example.Criteria friendReqCriteria = friendReq.createCriteria();
        friendReqCriteria.andEqualTo("sendUserId",sendUserId);
        friendReqCriteria.andEqualTo("replyUserId",accessUserId);
        friendsReqMapper.deleteByExample(friendReq);

    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void passFriendRequest(String accessUserId, String sendUserId) {
        saveFriend(accessUserId,sendUserId);
        saveFriend(sendUserId,accessUserId);
        deleteFriendRequest(accessUserId,sendUserId);
        // 推送到请求好友的客户端去刷新好友列表


        Channel sendChannel = UserChannelRel.get(sendUserId);
        if (sendChannel!=null){
            DataContent dataContent = new DataContent();
            dataContent.setAction(MsgActionEnum.PULL_FRIEND.type);
            sendChannel.writeAndFlush(new TextWebSocketFrame(JsonUtils.objectToJson(dataContent)));
        }

    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void saveFriend(String accessUserId, String sendUserId) {
        FriendRelation friendRelation = new FriendRelation();
        friendRelation.setOwnerId(accessUserId);
        friendRelation.setFriendId(sendUserId);
        friendRelation.setId(sid.nextShort());
        friendRelationMapper.insert(friendRelation);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<FriendVO> queryMyFriends(String userId) {
        return userInfoMapperCustom.queryMyFriends(userId);
    }

}
